package Login;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Test {
    public static void main(String[] args) throws  Exception{
        String StuName = "小向";
        String StuPwd = "123456";
        boolean flag = login(StuName,StuPwd);
        System.out.println(flag);
    }
    public  static  boolean login(String StuName,String StuPwd) throws  Exception{
        // 连接数据库，去查找  stu 表，有没有name = name ，pwd = pwd。


        //2.注册驱动
        Class.forName("com.mysql.jdbc.Driver");//mysql5之后的驱动jar包可以省略注册驱动的步骤
        //3.获取数据库连接对象
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=utf8", "root", "x");


        //5.获取执行sql的对象 stmt
        Statement stmt = conn.createStatement();

        //定义

        // 5 .定义sql
        // select * from stu where name ='zhangsan' and pwd = 'xxx';
        // String sql = "select * from stu where  name =  " + name +" and pwd =" + pwd ;
        String sql="select * from student where StuName ='"+StuName+"' and StuPwd = '"+StuPwd+"'";

        //得到ResultSet
        ResultSet resultSet= stmt.executeQuery(sql);
        // 得到结果集，往下取一行，如果得到true ，说明为真；
        return  resultSet.next();



    }
}
